package com.siqi.hr.system.custom.dao.mysql;

import com.siqi.hr.system.common.enums.OperationEnum;
import com.siqi.hr.system.custom.dao.intf.EntityOperationDao;
import com.siqi.hr.system.custom.entity.EntityOperation;
import com.siqi.hr.system.custom.entity.EntityRoleOperation;
import com.siqi.hr.system.custom.filter.EntityOperationFilter;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体权限操作dao实现层(主要处理操作按钮的增删改查)
 * Created by Administrator on 2016/8/16.
 * @author xiaogq
 * @since JDK1.8.0_12
 */
@Component("entityOperationDao")
public class EntityOperationDaoImpl implements EntityOperationDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<EntityOperation> queryAllOperation(EntityOperationFilter operationFilter) throws Exception {
        StringBuffer sql = new StringBuffer("select * from sys_org_operation where 1 = 1 ");
        if(StringUtils.isNotBlank(operationFilter.getName())){
            sql.append(" and name like '%").append(operationFilter.getName()).append("%'");
        }
        if (operationFilter.getSysOperation() != null){
            sql.append(" and sysOperation = ").append(operationFilter.getSysOperation());
        }
        if(StringUtils.isNotBlank(operationFilter.getShowHere())){
            sql.append(" and "+operationFilter.getShowHere()+" = true ");
        }
        if (StringUtils.isEmpty(operationFilter.getSort())){
            sql.append(" order by ").append(" position asc ");
        }else{
            sql.append(" order by ").append(operationFilter.getSort()).append(" ").append(operationFilter.getOrder());
        }
        sql.append(" limit ").append(operationFilter.getOffset()).append(",").append(operationFilter.getLimit());
        List<EntityOperation> list = jdbcTemplate.query(sql.toString(), new EntityOperation.getAllOperation());
        return list;
    }

    @Override
    public int queryAllOperationCounts(EntityOperationFilter operationFilter) throws Exception {
        StringBuffer sql = new StringBuffer("select count(id) from sys_org_operation where 1 = 1");
        if(StringUtils.isNotBlank(operationFilter.getName())){
            sql.append(" and name like '%").append(operationFilter.getName()).append("%'");
        }
        if (operationFilter.getSysOperation() != null){
            sql.append(" and sysOperation = ").append(operationFilter.getSysOperation());
        }
        if(StringUtils.isNotBlank(operationFilter.getShowHere())){
            sql.append(" and "+operationFilter.getShowHere()+" = true ");
        }
        int counts = jdbcTemplate.queryForObject(sql.toString(),Integer.class);
        return counts;
    }

    public List<EntityOperation> queryEntityOperationByHere(String roleId, int entityId,OperationEnum operationEnum)throws Exception{
        StringBuffer sql = new StringBuffer("select sys_op.id,sys_op.name,relation_op.role_id,relation_op.entity_id,");
        sql.append(" sys_op.operationType,sys_op.sysOperation,sys_op."+operationEnum.name()+" as isHere,'"+operationEnum.name()+"' as showHere, sys_op.method,sys_op.icon,sys_op.description ");
        sql.append(" from sys_org_role_entity_operation relation_op, sys_org_operation sys_op where relation_op.operation_id = sys_op.id ");
        sql.append(" and sys_op."+operationEnum.name()+" = true and relation_op.flag = true and relation_op.entity_id = ? and relation_op.role_id = ? ");
        List<EntityOperation> list = jdbcTemplate.query(sql.toString(),new Object[]{entityId, roleId}, new EntityOperation.getEntityRoleOperation());
        return list;
    }

    @Override
    public EntityOperation queryEntityOperationById(String id) throws Exception {
        StringBuffer sql = new StringBuffer("select * from sys_org_operation where id = '").append(id).append("'");
        EntityOperation operation = jdbcTemplate.queryForObject(sql.toString(),new EntityOperation.getAllOperation());
        return operation;
    }

    @Override
    public int addEntityOperation(EntityOperation operation) throws Exception {
        StringBuffer sql = new StringBuffer("insert into sys_org_operation (id,name,operationType,sysOperation,showList,showColumn,showView,permission,position,icon,description) values (?,?,?,?,?,?,?,?,?,?,?)");
        int numbers = jdbcTemplate.update(sql.toString(),
            new Object []{
                operation.getId(),
                operation.getName(),
                operation.getOperationType(),
                operation.isSysOperation(),
                operation.getShowList(),
                operation.getShowColumn(),
                operation.getShowView(),
                operation.getPermission(),
                operation.getPosition(),
                operation.getIcon(),
                operation.getDescription()
            }
        );
        return numbers;
    }

    @Override
    public int updateEntityOperation(EntityOperation operation) throws Exception {
        StringBuffer sql = new StringBuffer("update sys_org_operation set name = ?,showList = ?,showColumn = ?,showView = ?,description = ? where id = ?");
        int numbers = jdbcTemplate.update(sql.toString(),
                new Object []{
                    operation.getName(),
                    operation.getShowList(),
                    operation.getShowColumn(),
                    operation.getShowView(),
                    operation.getDescription(),
                    operation.getId()
                }
        );
        return numbers;
    }

    @Override
    public void deleteEntityOperation(String id) throws Exception {
        StringBuffer sql = new StringBuffer("delete from sys_org_operation where id = '").append(id).append("'");
        jdbcTemplate.execute(sql.toString());
    }

    @Override
    public List<EntityOperation> queryAllOperationByPms(Boolean bool){
        StringBuffer sql = new StringBuffer("select id,name from sys_org_operation where permission = "+bool);
        List<EntityOperation> resultOptList = new ArrayList<EntityOperation>();
        List<EntityOperation> operationList = jdbcTemplate.query(sql.toString(),new EntityOperation.getMainOperation());
        String id = "",name = "";
        for(EntityOperation opt : operationList){
            //如果是停用/启用按钮
            if(opt.getId().equals("002N89JLQW5H") || opt.getId().equals("002N89JiUvcl")){
                id += "/"+opt.getId();
                name += "/"+opt.getName();
                continue;
            }
            resultOptList.add(opt);
        }
        if(StringUtils.isNotBlank(id)){
            id = id.substring(1);
            name = name.substring(1);
            resultOptList.add(new EntityOperation(id,name));
        }
        return resultOptList;
    }

    @Override
    public List<Map<String, Object>> queryRoleOperationList(String roleid) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select entity_id as entityId,operation_id as oid from sys_org_role_entity_operation ");
        sql.append(" where flag = true and role_id = '").append(roleid).append("' ");
        List<Map<String,Object>> roleOperList = jdbcTemplate.queryForList(sql.toString());
        return roleOperList;
    }
}
